Create Project: springboot_service_class (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Class: MyService.java (inside package controllers)
MyService.java
package com.ivoronline.springboot_service_class_autowired.services;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String hello() {
return "Hello from Service";
}
}
MyController.java
package com.ivoronline.springboot_service_class_autowired.controllers;
import com.ivoronline.springboot_service_class_autowired.services.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired MyService myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
//CALL SERVICE
String result = myService.hello();
//RETURN RESULT
return result;
}
}